﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MSRoomLocator.ViewModels;

namespace MSRoomLocator
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            Visibility isLight = (Visibility)Resources["PhoneLightThemeVisibility"];
            Visibility isDark = (Visibility)Resources["PhoneDarkThemeVisibility"];
            App.ViewModel.Theme = (isLight == System.Windows.Visibility.Visible) ? "Light" : "Dark";

            if (App.ViewModel.Theme == "Light")
            {
                this.noRoomText.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            }
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }

            this.buildingList.ItemsSource = App.ViewModel.Buildings;
            roomList.ItemsSource = null;
            this.noRoomText.Visibility = System.Windows.Visibility.Collapsed;
            //this.BuildingSelected("114");
        }

        #region Methods
        void BuildingSelected(string name)
        {
            roomList.ItemsSource = null;
            App.ViewModel.SelectedBuilding = App.ViewModel.Buildings.FirstOrDefault(b => b.Name == name);
            buildingList.ItemsSource = App.ViewModel.Buildings.Where(b => b.Name == name);
            //roomList.ItemsSource = App.ViewModel.Rooms.Where(r => r.Building == name);
            List<RoomViewModel> rooms = App.ViewModel.GetRooms();

            if (rooms.Count == 0)
            {
                this.noRoomText.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.noRoomText.Visibility = System.Windows.Visibility.Collapsed;
                roomList.ItemsSource = rooms;
            }

            BuildingName.Text = string.Empty;
        }

        void RoomSelected(RoomViewModel room)
        {
            App.ViewModel.GoalRoom = room;
            NavigationService.Navigate(new Uri("/FloorPlan.xaml", UriKind.Relative));
        }
        #endregion

        #region User input events
        private void BuildingName_KeyUp(object sender, KeyEventArgs e)
        {
            this.buildingList.ItemsSource = App.ViewModel.Buildings.Where(b => b.Name.StartsWith(BuildingName.Text));
        }

        private void buildingEntry_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.BuildingSelected((sender as TextBlock).Text);
        }

        private void roomEntry_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.RoomSelected(roomList.SelectedItem as RoomViewModel);
        }

        #endregion

        private void BuildingName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.buildingList.ItemsSource = App.ViewModel.Buildings;
            roomList.ItemsSource = null;
            this.noRoomText.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}